package Mybitset;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: wulangaaa
 * Date: 2025-04-14
 * Time: 15:52
 */
public class MyBitSet {
    public int usedSize;
    private byte[] elem;


    public  MyBitSet(){
        //默认给数组的大小为1
        this.elem=new byte[1];
    }
    public MyBitSet(int n) {
        //n为总共有多少数据
        this.elem =new byte[n/8+1];
    }

    //插入方法
    public void set(int val){
    if (val<0){
        throw new ArrayIndexOutOfBoundsException();
    }
    int elemindex=val/8;
    int bitindex=val%8;
    //elem[elemindex]= (byte) (elem[elemindex] | (1<<bitindex));
    this.elem[elemindex] |= (1<<bitindex);
        usedSize++;
    }

    /**
     *
     * 查询是否有val这个数据
     * @param val
     * @return
     */
    public boolean get(int val){
        if (val<0){
            return false;
        }
        int elemindex=val/8;
        int bitindex=val%8;
        if ((elem[elemindex] & (1<<bitindex)) !=0){
            return true;
        }
        return false;
    }

    /**
     *
     * 等价于删除val--将1改为0
     * @param val
     */
    //
    public void reSetO(int val){
        if (val<0){
            throw  new ArrayIndexOutOfBoundsException();
        }
        int elemindex=val/8;
        int bitindex=val%8;
        this.elem[elemindex] &= ~(1<<bitindex);

    }

    /**
     * @return
     *
     * 获取1的个数  就是已经包含的数据的个数
     */
    public int getUsedSize(){
        return this.usedSize;
    }





}
